import 'package:flutter/material.dart';
import 'package:like_chat/blocs/friends_bloc.dart';
import 'package:like_chat/blocs/history_chat_bloc.dart';
import 'package:like_chat/entity/user.dart';
import 'package:like_chat/final_values.dart';
import 'package:like_chat/pages/details_page.dart';
import 'package:like_chat/pages/friend_list_page.dart';
import 'package:like_chat/pages/history_chat_page.dart';
import 'package:like_chat/pages/login_page.dart';
import 'package:like_chat/pages/search_bar_page.dart';
import 'package:like_chat/widget/loading_widget.dart';
import 'package:web_socket_channel/io.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _currentIndex = 0;
  User user;
  PageController _pageController;

  HistoryChatBloc _historyChatBloc;
  FriendsBloc _friendsBloc;

  IOWebSocketChannel _webSocketChannel;

  bool _isShow = true;

  @override
  void initState() {
    super.initState();
    _webSocketChannel = IOWebSocketChannel.connect('ws://echo.websocket.org')
      ..stream.listen(_listenMsg);

    _historyChatBloc = HistoryChatBloc();
    _friendsBloc = FriendsBloc();
    _pageController = PageController(initialPage: 0);
    //todo 从数据库里查询 User 表，如果有记录则无需跳转
  }

  @override
  Widget build(BuildContext context) {
    return user == null
        ? LoginPage(updateUser)
        : Scaffold(
            appBar: AppBar(
              title: const Text("Like Chat"),
              actions: <Widget>[
                IconButton(
                  icon: const Icon(Icons.search),
                  onPressed: () => showSearch(
                      context: context, delegate: SearchBarDelegate()),
                ),
                IconButton(
                  icon: const Icon(Icons.add),
                  onPressed: () {},
                )
              ],
            ),
            body: PageView(
              controller: _pageController,
              onPageChanged: _onTap,
              children: <Widget>[
                HistoryChatPage(
                    valueChanged: _changeIsShow,
                    user: user,
                    bloc: _historyChatBloc,
                    webSocketChannel: _webSocketChannel,
                ),
                FriendListPage(
                  user: user,
                  bloc: _friendsBloc,
                ),
                DetailsPage(user: user),
              ],
            ),
            bottomNavigationBar: BottomNavigationBar(
                items: [
                  BottomNavigationBarItem(
                      icon: Icon(Icons.message), title: Text("聊天")),
                  BottomNavigationBarItem(
                      icon: Icon(Icons.people), title: Text("朋友")),
                  BottomNavigationBarItem(
                      icon: Icon(Icons.person), title: Text("我的")),
                ],
                onTap: _onTap,
                currentIndex: _currentIndex,
                fixedColor: Colors.red),
          );
  }

  void _changeIsShow(bool flag) {
    setState(() {
      _isShow = flag;
    });
  }

  void updateUser() {
    setState(() {
      user = new User();
    });
  }

  void _onTap(int value) {
    setState(() {
      _currentIndex = value;
      _pageController.animateToPage(value,
          duration: Duration(milliseconds: 1), curve: Curves.ease);
    });
  }

  @override
  void dispose() {
    _historyChatBloc.dispose();
    _webSocketChannel.sink.close();
    super.dispose();
  }

  _listenMsg(msg) {
    print("socket: $msg");
  }
}
